(function(){
//#requires jDev, jDev.event
var $event = jDev.event;

var $ui = jDev.module('jDev.ui', {

  _viewFile: 'ui.html',
  
  _id: 'jDev',
  
  _object: null,
  _viewport: null,
  _element: null,
  _elementStyle: null,
  
  _toolbar: null,
  _consoleOut: null,
  _consoleIn: null,
  _commandLine: null,
  _consoleStyle: null,
  
  // Frame
  frame: {
    _element: null,
    _viewport: null,
    _document: null,
    
    _isVisible: false, 
    _scrollHandler: null,
    
    create: function(){
      var el = $ui.frame._element = document.createElement('iframe');
      var style = el.style;
       
      el.id = 'jDevFrame';
      el.src = jDev.viewURL + $ui._viewFile;
      el.setAttribute('frameBorder', '0');
      
      //style.zIndex = '2147483647'; // max
      style.zIndex = '1147483647';
      style.position = document.all ? 'absolute' : 'fixed';
      style.width = document.body.clientWidth + 'px';
      style.left = '0';
      style.bottom = '-1px';
      style.visibility = 'visible';    
      style.height = jDev.settings.windowHeight;
      
      document.body.appendChild(el);
    },
    
    onReady: function(doc){
      var obj = $ui.frame;
      
      obj._controllers = [
          [window, 'resize', obj.draw],
          [window, 'unload', obj.destroy]
        ];
        
      if (jDev.isIE) {
        obj._scrollHandler = [window, 'scroll', obj.fixPosition];
        //$event.add.apply(this, obj._scrollHandler);
      }

      $ui.initialize(obj, doc, document.body);
    },
    
    draw: function(){
      var height =  $ui._element.clientHeight - 
                    $ui._toolbar.offsetHeight - 
                    $ui._consoleIn.clientHeight;
                     
      height = Math.max(height, 0);
      
      $ui._consoleStyle.height = height + 'px';
      $ui._elementStyle.width = $ui._viewport.clientWidth + 'px';
    },
    
    fixPosition: function(){
      var maxHeight = document.body.clientHeight;
      
      //var height = $ui._maximized ? maxHeight-1 : $ui._height;
      var height = jDev.settings.windowHeight;
      
      $ui._elementStyle.top = maxHeight - height + document.body.scrollTop + 'px'; 
    },
    
    destroy: function(){
      $ui.finalize($ui.frame);
    }    
  },
  
  // Popup
  popup: {
    _element: null,
    _viewport: null,
    _document: null,
    
    _controllers: null,
    
    create: function(){
      var url = jDev.viewURL + $ui._viewFile;
      var height = jDev.settings.windowHeight;
      var options = [
          'true,top=',
          Math.max(screen.height - height, 0),
          ',left=0,height=',
          height,
          ',width=',
          screen.width,
          ',resizable'          
        ].join('');
      
      
      //var popup = $ui.popup;
      var popup = $ui.popup._element = window.open(
          jDev.viewURL + $ui._viewFile, 
          'popup', 
          options
        );
      
      if (popup) {
        popup.focus();
      } else {
        $ui.popup._element = null;
        alert('Desabilite o bloqueador de popups para abrir o console em outra janela!')
      }
    },
    
    onReady: function(doc){
      var obj = $ui.popup;
      
      doc.body.id = 'popup';
      
      obj._controllers = [
          [obj._element.window, 'resize', obj.draw],
          [obj._element.window, 'unload', obj.destroy]
        ];

      $ui.initialize(obj, doc, doc.body);
    },
    
    draw: function(){
      var height = $ui._viewport.clientHeight - 
                   $ui._toolbar.offsetHeight - 
                   $ui._consoleIn.clientHeight;

      height = Math.max(height-2, 0);
      
      $ui._consoleStyle.height = height + 'px';
    },
    
    destroy: function(){
      $ui.finalize($ui.popup);
      
      var o = $ui.frame;
      if(o._element){
        $ui.initialize(o, o._document, o._viewport);
      }
    }
  },  
  
  initialize: function(obj, doc, viewport){
    obj._document    = doc;
    obj._viewport    = viewport;
    
    $ui._viewport    = viewport;
    $ui._object      = obj;
    
    $ui._toolbar     = doc.getElementById('toolbar');
    $ui._consoleOut  = doc.getElementById('consoleOut');
    $ui._consoleIn   = doc.getElementById('consoleIn');  
    $ui._commandLine = doc.getElementById('commandLine');
    
    $ui._element      = obj._element;
    $ui._elementStyle = obj._element.style;
    $ui._consoleStyle = $ui._consoleOut.style;
    
    var controllers = obj._controllers;
    if(controllers){
      for(var i=0, ci; ci=controllers[i]; i++){
        $event.add.apply(this, ci);
      }
    }
    
    forcingBars(doc);
    setTimeout(obj.draw, 0);
  },
  
  finalize: function(obj){
    obj._element      = null;
    $ui._viewport    = null;
    $ui._object      = null;
    $ui._toolbar     = null;
    $ui._consoleOut  = null;
    $ui._consoleIn   = null;  
    $ui._commandLine = null;
    $ui._element      = null;
    $ui._elementStyle = null;
    $ui._consoleStyle = null;
    
    var controllers = obj._controllers;
    if(controllers){
      for(var i=0, ci; ci=controllers[i]; i++){
        $event.remove.apply(this, ci);
      }
    }
  },
  
  //
  toggle: function(forceOpen, popup){
  
    if(popup){

      var obj = $ui.popup; 
      if(!obj._element){
      
        if ($ui.frame._element){
          $ui.frame._isVisible = false;
          $ui._elementStyle.display = 'none';
        }
        
        setTimeout(obj.create, 0);
      }
    
    } else if (!$ui.popup._element) {
    
      var obj = $ui.frame; 
      obj._isVisible = forceOpen || !obj._isVisible;
       
      if(obj._element){
        
        if(obj._isVisible){
          $ui._elementStyle.display = 'block';
          obj.draw();
        } else {
          $ui._elementStyle.display = 'none';
        }
        
      } else {
        obj.create();
      }
    }
  },
  
  // Event handlers
  onDocumentReady: function(){
    $event.add(
        document, 
        jDev.isIE || jDev.isSafari ? 'keydown' : 'keypress', 
        $ui.onKeyDown
      );
  },
  
  onKeyDown: function(event){
    if (event.keyCode == 123 /* F12 */) {
      if (event.shiftKey) {
        $ui.toggle(false, event.ctrlKey);
      } /*else {
        $ui.toggleFirebug();
      }*/
    } else {
      return;
    }
    
    $event.stop(event);
  }
  
});

jDev.onReady($ui.onDocumentReady);

})();